/**
 * 
 */
package com.quanyan.place.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.CommonUtils;
import com.quanyan.orderpay.request.ReqOrderRefund;
import com.quanyan.orderpay.request.ReqRefundPayInfo;
import com.quanyan.orderpay.request.ReqRefundProduct;
import com.quanyan.orderpay.response.RespRefund;
import com.quanyan.orderpay.service.OrderPayService;
import com.quanyan.pay.enums.PayWayEnum;
import com.quanyan.pay.enums.RefundStatusEnum;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbAdminUser;
import com.quanyan.place.entity.TbBizPlaceMemberCard;
import com.quanyan.place.entity.TbBizPlaceMemberCardExample;
import com.quanyan.place.entity.TbBizPlaceMemberOpHistory;
import com.quanyan.place.entity.TbBizPlaceMemberOpHistoryExample;
import com.quanyan.place.entity.TbBizPlaceOrder;
import com.quanyan.place.entity.TbBizPlaceOrderExample;
import com.quanyan.place.entity.TbBizPlaceOrderItem;
import com.quanyan.place.entity.TbBizPlaceOrderItemExample;
import com.quanyan.place.entity.TbBizPlaceOrderItemExample.Criteria;
import com.quanyan.place.entity.TbBizPlacePayItem;
import com.quanyan.place.entity.TbBizPlacePayItemExample;
import com.quanyan.place.entity.TbBizPlacePrivateOrder;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlan;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlanExample;
import com.quanyan.place.entity.TbBusinessLog;
import com.quanyan.place.entity.apireq.PlaceRefundDetail;
import com.quanyan.place.entity.apireq.PlaceRefundInfo;
import com.quanyan.place.entity.apireq.PlaceRefundItem;
import com.quanyan.place.entity.apireq.ReqOrderHistory;
import com.quanyan.place.entity.apireq.ReqOrderOperate;
import com.quanyan.place.entity.apireq.ReqPlaceOrderListVo;
import com.quanyan.place.entity.apireq.ReqPlaceOrderSearchParam;
import com.quanyan.place.entity.apireq.ReqPlaceRefundParam;
import com.quanyan.place.entity.apiresp.RespFixedChildOrder;
import com.quanyan.place.entity.apiresp.RespOrderItemDiscount;
import com.quanyan.place.entity.apiresp.RespPlaceOrderDiscountItem;
import com.quanyan.place.entity.apiresp.RespPlaceOrderList;
import com.quanyan.place.entity.vo.UnitSalePlan;
import com.quanyan.place.mapper.PlaceOrderAdminMapper;
import com.quanyan.place.mapper.RefundOrderMapper;
import com.quanyan.place.mapper.TbAdminUserMapper;
import com.quanyan.place.mapper.TbBizPlaceMemberCardMapper;
import com.quanyan.place.mapper.TbBizPlaceMemberOpHistoryMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderItemMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderMapper;
import com.quanyan.place.mapper.TbBizPlacePayItemMapper;
import com.quanyan.place.mapper.TbBizPlacePrivateOrderMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitSalePlanMapper;
import com.quanyan.place.service.PlaceGoodsService;
import com.quanyan.place.service.PlaceOrderAdminService;
import com.quanyan.place.service.PlaceOrderService;
import com.quanyan.place.service.PlaceOrderUtilService;
import com.quanyan.place.service.PlaceTicketService;
import com.quanyan.place.service.TbBusinessLogService;
import com.quanyan.place.wrapper.PlaceOrderAdminServiceWrapper;
import com.quanyan.place.wrapper.PlaceOrderServiceWrapper;

/**
 * @des 商家端订单管理(new)
 * @author tim
 * @date 2017年4月12日
 */
@Service("placeOrderAdminService")
public class PlaceOrderAdminServiceImpl implements PlaceOrderAdminService {

	private static final Logger logger = LoggerFactory.getLogger(PlaceOrderAdminServiceImpl.class);
	@Autowired
	PlaceOrderAdminMapper placeOrderAdminMapper;
	@Autowired
	TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;
	@Autowired
	TbBizPlaceOrderMapper tbBizPlaceOrderMapper;
	@Autowired
	TbBizPlaceOrderItemMapper tbBizPlaceOrderItemMapper;
	@Autowired
	TbBizPlacePrivateOrderMapper privateOrderMapper;
	@Autowired
	TbAdminUserMapper tbAdminUserMapper;
	@Autowired
	PlaceOrderService placeOrderService;
	@Autowired
	PlaceTicketService placeTicketService;
	@Autowired
	TbBusinessLogService tbBusinessLogService;
	@Autowired
	PlaceOrderAdminServiceWrapper placeOrderAdminServiceWrapper;
	@Autowired
	TbBizPlacePayItemMapper tbBizPlacePayItemMapper;
	@Autowired
	TbBizPlacePrivateOrderMapper tbBizPlacePrivateOrderMapper;

	@Autowired
	TbBizPlaceMemberOpHistoryMapper tbBizPlaceMemberOpHistoryMapper;

	@Autowired
	TbBizPlaceMemberCardMapper tbBizPlaceMemberCardMapper;

	@Autowired
	PlaceOrderUtilService placeOrderUtilService;

	@Autowired
	PlaceGoodsService placeGoodsService;

	@Autowired
	OrderPayService orderPayService;
	
	@Autowired
	RefundOrderMapper refundOrderMapper;

	@Override
	public PageObj<List<RespPlaceOrderList>> getPlaceOrderList(ReqPlaceOrderSearchParam orderSearchParam) {
		Integer pageSize = orderSearchParam.getPageSize();
		Integer pageNum = orderSearchParam.getPageNum();
		// 关键字搜索：只设置一个输入框，提示文字显示： 订单号、会员卡号精确搜索；顾客姓名全模糊搜索、手机号尾号4位模糊搜索 check
		if (orderSearchParam.getKeyWords() != null) {
			if (orderSearchParam.getKeyWords().trim().length() == 21) {
				orderSearchParam.setPlaceOrderId(orderSearchParam.getKeyWords().trim());
			} else if (orderSearchParam.getKeyWords().trim().length() == 18) {
				orderSearchParam.setInnerCardNo(orderSearchParam.getKeyWords().trim());
			} else if (orderSearchParam.getKeyWords().trim().length() == 11) {// mobile
																				// 手机号截取后4位
				orderSearchParam.setMobile(orderSearchParam.getKeyWords().trim());
			} else {
				orderSearchParam.setCustomerName(orderSearchParam.getKeyWords().trim());
			}
		}

		if (null == pageNum) {
			pageNum = 0;
			orderSearchParam.setPageNum(pageNum);
		}
		if (null == pageSize) {
			pageSize = Constants.DEFAULT_PAGE_SIZE;
			orderSearchParam.setPageSize(pageSize);
		}

		List<RespPlaceOrderList> placeOrderLists = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();
		List<RespOrderItemDiscount> orderItemDiscounts = new ArrayList<>();
		int total = placeOrderAdminMapper.getPlaceOrderCount(orderSearchParam);
		orderSearchParam.setPage(new Page(pageNum * pageSize, pageSize, total));
		placeOrderLists.addAll(placeOrderAdminMapper.getPlaceOrderList(orderSearchParam));

		int temp = 0;
		int eggCount = 0;
		BigDecimal discountAdjust = new BigDecimal(0);
		BigDecimal memberDiscount = new BigDecimal(0);// 会员优惠
		BigDecimal discountCoupon = new BigDecimal(0);// 优惠券
		BigDecimal credit = new BigDecimal(0);// 应付金
		for (RespPlaceOrderList respPlaceOrderList : placeOrderLists) {

			params.put("placeOrderId", respPlaceOrderList.getPlaceOrderId());
			orderItemDiscounts.addAll(placeOrderAdminMapper.getOrderDiscountItem(params));

			for (RespOrderItemDiscount orderItemDiscount : orderItemDiscounts) {
				eggCount += orderItemDiscount.getEggCount();
				discountAdjust = discountAdjust.add(orderItemDiscount.getDiscountAdjust());
				credit = credit.add(orderItemDiscount.getSalePrice());
				memberDiscount = memberDiscount.add(orderItemDiscount.getDiscountDiscountMember())
						.add(orderItemDiscount.getDiscountMember());
				discountCoupon = discountCoupon.add(orderItemDiscount.getDiscountCoupon())
						.add(orderItemDiscount.getDiscountDiscountCoupon());
				if (orderItemDiscount.getUseType() == 0 || orderItemDiscount.getUseType() == 3) {// 商品类型:0：开单
																									// 3：app开单

					// ???
				} else if (orderItemDiscount.getUseType() == 4) {// 多种经营

				}

			}

			placeOrderLists.get(temp).setEggCount(eggCount);
			placeOrderLists.get(temp).setDiscountAdjust(discountAdjust);
			placeOrderLists.get(temp).setCredit(credit);
			placeOrderLists.get(temp).setDiscountCoupon(discountCoupon);
			placeOrderLists.get(temp).setMemberDiscount(memberDiscount);
			temp++;
		}

		PageObj<List<RespPlaceOrderList>> result = PageObj.create(total, pageNum, pageSize, placeOrderLists);
		return result;
	}

	@Override
	public int addOrderStar(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return placeOrderAdminMapper.addOrderStar(params);
	}

	@Override
	public String printTicket(String placeOrderId) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public int updateCasualMemberOrder(Map<String, Object> params) {
		return placeOrderAdminMapper.updateCasualMemberOrder(params);
	}

	
	@Override
	public RespFixedChildOrder searchFixedAllChildOrder(ReqPlaceOrderSearchParam orderSearchParam) {
		Integer pageSize = orderSearchParam.getPageSize();
		Integer pageNum = orderSearchParam.getPageNum();
		if (null == pageNum) {
			pageNum = 0;
		}
		if (null == pageSize) {
			pageSize = Constants.DEFAULT_PAGE_SIZE;
		}

		Integer parentId = null;
		RespFixedChildOrder respFixedChildOrder = new RespFixedChildOrder();
		Map<String, Object> params = new HashMap<>();
		params.put("placeOrderId", orderSearchParam.getPlaceOrderId());
		parentId = placeOrderAdminMapper.getParentOrderId(params);// 获取父订单id

		if (parentId != null) {
			TbBizPlacePrivateOrder privateOrder = privateOrderMapper.selectByPrimaryKey(parentId);// 获取父订单信息
			TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(privateOrder.getCreateUid());
			if (tbAdminUser != null) {
				respFixedChildOrder.setOperator(tbAdminUser.getLoginName());
			} else {
				respFixedChildOrder.setOperator("");
			}
			respFixedChildOrder.setOrderNotes(privateOrder.getDescrible());
			respFixedChildOrder.setOrderTime(privateOrder.getOrderTime());
			respFixedChildOrder.setUseType(2);// 固定场
			respFixedChildOrder.setSaleChannel((byte) 4);// 销售渠道(包场只能针对商家端)
			respFixedChildOrder.setPayType("详见每个子订单");

			List<RespPlaceOrderList> placeOrderLists = new ArrayList<>();
			List<TbBizPlaceOrder> placeOrders = getPlaceOrder(parentId); // 获取子订单信息
			for (TbBizPlaceOrder placeOrder : placeOrders) {
				RespPlaceOrderList respPlaceOrderList = new RespPlaceOrderList();
				respPlaceOrderList.setPaymentTime(placeOrder.getPaymentTime());
				respPlaceOrderList.setPlaceOrderId(placeOrder.getPlaceOrderId());
				respPlaceOrderList.setChannel(placeOrder.getOrderChannel());
				respPlaceOrderList.setUseType(placeOrder.getUseType());
				respPlaceOrderList.setCustomerName(placeOrder.getCustomerName());
				respPlaceOrderList.setMobile(placeOrder.getMobile());
				respPlaceOrderList.setOrderStatus(placeOrder.getOrderStatus());

				// 获取 订单明细
				List<TbBizPlaceOrderItem> orderItems = new ArrayList<>();
				TbBizPlaceOrderItemExample example = new TbBizPlaceOrderItemExample();
				Criteria criteria = example.createCriteria();
				// criteria.??
				orderItems = tbBizPlaceOrderItemMapper.selectByExample(example);
				BigDecimal totalAmount = new BigDecimal(0.0);
				BigDecimal totalPay = new BigDecimal(0.0);
				for (TbBizPlaceOrderItem orderItem : orderItems) {
					// totalAmount=totalAmount.add(augend);??
					// totalPay=totalPay.add(augend);
				}
				respPlaceOrderList.setOrderItems(orderItems);
				// respPlaceOrderList.setPayType(payType);//?
				respPlaceOrderList.setAmount(totalAmount);// 实付
				respPlaceOrderList.setCredit(totalPay);// 应付

				placeOrderLists.add(respPlaceOrderList);
			}
			// orderSearchParam.setPage(new Page(pageNum * pageSize, pageSize,
			// placeOrders.size()));
			// placeOrderLists.addAll(placeOrderAdminMapper.getPlaceOrderList(orderSearchParam));
			PageObj<List<RespPlaceOrderList>> placeOrderList = PageObj.create(placeOrders.size(), pageNum, pageSize,
					placeOrderLists);
			respFixedChildOrder.setPlaceOrderList(placeOrderList);

			// respFixedChildOrder.setTotalPay(totalPay);
			// respFixedChildOrder.setChildOrderSize(childOrderSize);

		}

		return respFixedChildOrder;
	}

	private List<TbBizPlaceOrder> getPlaceOrder(Integer parentId) {
		TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
		TbBizPlaceOrderExample.Criteria tbBizPlaceOrderExampleCriteria = tbBizPlaceOrderExample.createCriteria();
		tbBizPlaceOrderExampleCriteria.andParentIdEqualTo(parentId);
		List<TbBizPlaceOrder> selectByExample = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
		return selectByExample;
	}

	
	@Override
	public APIResponse<?> placeRefund(PlaceRefundInfo reqPlaceRefundInfo)
			throws Exception {
		if (StringUtils.isEmpty(reqPlaceRefundInfo.getPlaceOrderId()) && !StringUtils.isEmpty(reqPlaceRefundInfo.getCustomerId())) {
			return APIResponse.returnFail("退款失败,订单号,Uid,placeId不能为空！");
		}
		
		// 查询订单
		TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
		tbBizPlaceOrderExample.createCriteria().andPlaceOrderIdEqualTo(reqPlaceRefundInfo.getPlaceOrderId());
		List<TbBizPlaceOrder> tbBizPlaceOrders = this.tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
		// 查询订单支付方式
		TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
		tbBizPlacePayItemExample.createCriteria().andPlaceOrderIdEqualTo(reqPlaceRefundInfo.getPlaceOrderId());
		
		// 订单明细
		TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
		tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(reqPlaceRefundInfo.getPlaceOrderId());
		
		//优惠明细
		Map params=new HashMap<>();
		params.put("placeOrderId", reqPlaceRefundInfo.getPlaceOrderId());
		if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_PART){
			tbBizPlacePayItemExample.createCriteria().andOrderItemUuidEqualTo(reqPlaceRefundInfo.getItemUuid());
			tbBizPlaceOrderItemExample.createCriteria().andItemUuidEqualTo(reqPlaceRefundInfo.getItemUuid());
			params.put("itemUuid", reqPlaceRefundInfo.getItemUuid());
		}
		
		List<TbBizPlacePayItem> tbBizPlacePayItems = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
		List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
		List<RespPlaceOrderDiscountItem> orderDiscountItems=placeOrderAdminMapper.getDiscountItem(params);
		
		if (CollectionUtils.isEmpty(tbBizPlaceOrders)) {
			return APIResponse.returnFail("查不到此订单！");
		}
		if (CollectionUtils.isEmpty(tbBizPlacePayItems)) {
			return APIResponse.returnFail("支付明细为空！");
		}
		if (CollectionUtils.isEmpty(tbBizPlaceOrderItems)) {
			return APIResponse.returnFail("订单明细为空！");
		}

		TbBizPlaceOrder tbBizPlaceOrder = tbBizPlaceOrders.get(0);
		if(tbBizPlaceOrder.getOrderStatus().equals(PlaceConstants.ORDER_COMPLETE_PAY_STATUS)){
			return APIResponse.returnFail("已完成状态订单不退！");
		}
		
		// 未付款 未支付
		if (tbBizPlaceOrder.getPayStatus().equals(PlaceConstants.ORDER_PREPAY_STATUS)
				&& tbBizPlaceOrder.getOrderStatus().equals(PlaceConstants.NOT_PAY_STATUS)) {
			// 订场业务才可以更改库存
			if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
					|| tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
					|| tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)) {
				boolean flag = placeOrderService.cancelBook(tbBizPlaceOrder.getPlaceOrderId());
				if (flag) {
					return APIResponse.returnSuccess("取消预订成功！");
				}else{
					throw new BizException("取消预订失败");
				}
			}
		}
		// 商家端订单退款
		this.adminOrderRefund(tbBizPlaceOrder, tbBizPlaceOrderItems, tbBizPlacePayItems,reqPlaceRefundInfo,orderDiscountItems);
		return APIResponse.returnSuccess();
	}

	
	private void adminOrderRefund(TbBizPlaceOrder tbBizPlaceOrder, List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,
			List<TbBizPlacePayItem> tbBizPlacePayItems, PlaceRefundInfo reqPlaceRefundInfo,List<RespPlaceOrderDiscountItem> orderDiscountItems) throws Exception {
		if (tbBizPlaceOrder.getPayStatus().intValue() != PlaceConstants.PAY_STATUS.intValue()
				&& tbBizPlaceOrder.getOrderStatus().intValue() != PlaceConstants.ORDER_PAYED_STATUS) {
			throw new BizException("该订单未支付不能退款");
		}
		BigDecimal refundPrice = new BigDecimal(0);
		//1. 修改订单状态
		TbBizPlaceOrder newTbBizPlaceOrder = new TbBizPlaceOrder();

		BeanUtils.copyProperties(tbBizPlaceOrder, newTbBizPlaceOrder);
		newTbBizPlaceOrder.setId(tbBizPlaceOrder.getId());
		newTbBizPlaceOrder.setPlaceOrderId(reqPlaceRefundInfo.getPlaceOrderId());
		newTbBizPlaceOrder.setOrderStatusChangeDate(new Date());
		
		if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_ALL){//全部退
			if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())
					|| PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN
							.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
				// 扫码退款 订单状态 = 退款中
				newTbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUNDING_STATUS);
			} else {
				newTbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
				newTbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
				
			}
			for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems) {
				refundPrice = refundPrice.add(tbBizPlaceOrderItem.getSalePrice());
			}
		}else{//部分退
			if(tbBizPlaceOrder.getCredit()!=null){
				refundPrice.add(tbBizPlaceOrder.getCredit()).add(reqPlaceRefundInfo.getRefundAmount());
			}else{
				refundPrice.add(reqPlaceRefundInfo.getRefundAmount());
			}
			
		}
		
		newTbBizPlaceOrder.setCredit(refundPrice);// 退款金额
		if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)) {// 门票退款
			newTbBizPlaceOrder = this.ticketOrderRefund(tbBizPlaceOrderItems, newTbBizPlaceOrder, tbBizPlacePayItems,orderDiscountItems,reqPlaceRefundInfo);
		} else if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
				|| tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)) {// 开单包场退款
			newTbBizPlaceOrder = this.billingAndBlockOrderRefund(tbBizPlaceOrderItems, newTbBizPlaceOrder,
					tbBizPlacePayItems,orderDiscountItems,reqPlaceRefundInfo.getRefundType(),reqPlaceRefundInfo);
		} else if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)) {// 小商品退款
			// 小商品退款
			newTbBizPlaceOrder = this.goodsOrderRefund(tbBizPlaceOrderItems, newTbBizPlaceOrder, tbBizPlacePayItems,orderDiscountItems,reqPlaceRefundInfo);
		}else if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_SHOPPING_CART)){//购物车
			newTbBizPlaceOrder=this.shoppingCartRefund(tbBizPlaceOrderItems, newTbBizPlaceOrder, tbBizPlacePayItems,orderDiscountItems,reqPlaceRefundInfo);
		}

		try {
			// 退款日志
			TbBusinessLog log = new TbBusinessLog();
			log.setEntityId(tbBizPlaceOrder.getId());
			log.setEntityType(PlaceConstants.LOG_TYPE_PLACE_ORDER);
			log.setUid(reqPlaceRefundInfo.getOperateUid());
			log.setLogName("商家端退款");
			log.setLogInfo("订单号：[" + tbBizPlaceOrder.getPlaceOrderId() + "]" + " 退款人：["
					+ tbBizPlaceOrder.getCustomerName() + "],退款金额：[" + tbBizPlaceOrder.getCredit() + "], 电话：["
					+ tbBizPlaceOrder.getMobile() + "]");
			tbBusinessLogService.insertLogInfo(log);
		} catch (Exception e) {
			throw new Exception("退款日志生成失败.");
		}
		//已退款不能修改订单状态，需做判断
		if (this.tbBizPlaceOrderMapper.updateByPrimaryKeySelective(newTbBizPlaceOrder) == 0) {
			throw new Exception("修改订单异常");
		}

		// 包场子订单退款 更新父订单状态
		if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
				&& PlaceConstants.ORDER_REFUND_STATUS.equals(newTbBizPlaceOrder.getOrderStatus())) {
			placeOrderService.checkBlockOrder(tbBizPlaceOrder, PlaceConstants.ORDER_REFUND_STATUS, refundPrice);
		}
		// 添加会员支付后退款(退到会员卡)
		boolean numCardRefund = false;// 次卡退款标记
		int refundNum = 0;// 次卡退款次数
		// 获取会员卡
		TbBizPlaceMemberOpHistoryExample example = new TbBizPlaceMemberOpHistoryExample();
		TbBizPlaceMemberOpHistoryExample.Criteria criteria = example.createCriteria();
		criteria.andOrderNoEqualTo(reqPlaceRefundInfo.getPlaceOrderId());
		example.setOrderByClause(" id for update ");
		List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList = this.tbBizPlaceMemberOpHistoryMapper
				.selectByExample(example);
		if (CollectionUtils.isNotEmpty(tbBizPlaceMemberOpHistoryList)) {
			String innerCardNo = tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo();
			if (!StringUtils.isEmpty(innerCardNo)) {
				TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
				TbBizPlaceMemberCardExample.Criteria tbBizPlaceMemberCardCriteria = tbBizPlaceMemberCardExample
						.createCriteria();
				tbBizPlaceMemberCardCriteria.andInnerCardNoEqualTo(innerCardNo);
				tbBizPlaceMemberCardExample.setOrderByClause(" id for update ");
				List<TbBizPlaceMemberCard> tbBizPlaceMemberCardList = this.tbBizPlaceMemberCardMapper
						.selectByExample(tbBizPlaceMemberCardExample);
				TbBizPlaceMemberCard tbBizPlaceMemberCard = tbBizPlaceMemberCardList.get(0);
				if (PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER.equals(tbBizPlaceMemberCard.getCardType())) {
					// 次卡退款
					tbBizPlaceMemberCard.setSurplusNumbers(tbBizPlaceMemberCard.getSurplusNumbers()
							+ tbBizPlaceMemberOpHistoryList.get(0).getOpCardCount());
					numCardRefund = true;
					refundNum = tbBizPlaceMemberOpHistoryList.get(0).getOpCardCount();
				} else {
					// 储值，折扣卡，储值折扣卡退款
					tbBizPlaceMemberCard.setSurplusAmount(tbBizPlaceMemberCard.getSurplusAmount()
							.add(tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeAmount()
									.subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpAfterAmount())));
					tbBizPlaceMemberCard.setSurplusGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount()
							.add(tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeGiveAmount()
									.subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpAfterGiveAmount())));
				}
				int flag1 = tbBizPlaceMemberCardMapper.updateByPrimaryKey(tbBizPlaceMemberCard);
				if (flag1 == 1) {
					TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory = new TbBizPlaceMemberOpHistory();
					// 退款生成订单号
					String orderNo = CommonUtils.generateBusinessOrderNo(Constants.PLACE_SYSTEM, reqPlaceRefundInfo.getOperateUid());
					tbBizPlaceMemberOpHistory.setOrderNo(orderNo);
					tbBizPlaceMemberOpHistory.setRefundNo(tbBizPlaceMemberOpHistoryList.get(0).getOrderNo());
					tbBizPlaceMemberOpHistory.setId(tbBizPlaceMemberOpHistoryList.get(0).getId());
					tbBizPlaceMemberOpHistory.setCreateUid(tbBizPlaceMemberOpHistoryList.get(0).getCreateUid());
					tbBizPlaceMemberOpHistory.setPlaceId(tbBizPlaceMemberOpHistoryList.get(0).getPlaceId());
					tbBizPlaceMemberOpHistory.setUpdateTime(new Date());
					tbBizPlaceMemberOpHistory.setAmount(tbBizPlaceMemberOpHistoryList.get(0).getAmount());
					tbBizPlaceMemberOpHistory.setCreateTime(new Date());
					tbBizPlaceMemberOpHistory.setInnerCardNo(tbBizPlaceMemberCard.getInnerCardNo());
					tbBizPlaceMemberOpHistory
							.setNo(CommonUtils.generateNo(tbBizPlaceMemberOpHistoryList.get(0).getPlaceId()));
					if (tbBizPlaceMemberOpHistoryList.get(0).getOpTypeName().indexOf("抵扣") != -1) {
						tbBizPlaceMemberOpHistory.setOpTypeName(
								tbBizPlaceMemberOpHistoryList.get(0).getOpTypeName().replace("抵扣", "退款"));
					} else {
						tbBizPlaceMemberOpHistory
								.setOpTypeName(tbBizPlaceMemberOpHistoryList.get(0).getOpTypeName() + "退款");
					}
					tbBizPlaceMemberOpHistory.setUid(tbBizPlaceMemberOpHistoryList.get(0).getUid());
					tbBizPlaceMemberOpHistory.setUserName(tbBizPlaceMemberOpHistoryList.get(0).getUserName());
					tbBizPlaceMemberOpHistory.setMobile(tbBizPlaceMemberOpHistoryList.get(0).getMobile());
					tbBizPlaceMemberOpHistory.setCardTypeName(tbBizPlaceMemberOpHistoryList.get(0).getCardTypeName());
					tbBizPlaceMemberOpHistory.setMemo(tbBizPlaceMemberOpHistoryList.get(0).getMemo());
					tbBizPlaceMemberOpHistory.setOpAfterAmount(tbBizPlaceMemberCard.getSurplusAmount());
					tbBizPlaceMemberOpHistory.setOpAfterGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount());
					tbBizPlaceMemberOpHistory.setState(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_YES);
					tbBizPlaceMemberOpHistory.setOpBeforeAmount(tbBizPlaceMemberCard.getSurplusAmount()
							.subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeAmount()
									.subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpAfterAmount())));
					tbBizPlaceMemberOpHistory.setOpBeforeGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount()
							.subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeGiveAmount()
									.subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpAfterGiveAmount())));
					tbBizPlaceMemberOpHistory.setSourceType(PlaceConstants.SOURCE_TYPE_SCENEPAY);
					tbBizPlaceMemberOpHistory.setSrc(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_REFUND);
					tbBizPlaceMemberOpHistory.setType(PlaceConstants.PLACE_MEMBER_HISTORY_TYPE_RECHARGE);
					tbBizPlaceMemberOpHistory.setCardTypeId(tbBizPlaceMemberOpHistoryList.get(0).getCardTypeId());
					tbBizPlaceMemberOpHistory.setOpBeforeNumbers(tbBizPlaceMemberCard.getSurplusNumbers()
							- (tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeNumbers()
									- tbBizPlaceMemberOpHistoryList.get(0).getOpAfterNumbers()));
					tbBizPlaceMemberOpHistory.setOpAfterNumbers(tbBizPlaceMemberCard.getSurplusNumbers());
					if (PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER.equals(tbBizPlaceMemberCard.getCardType())) {
						tbBizPlaceMemberOpHistory.setOpCardCount(tbBizPlaceMemberOpHistoryList.get(0).getOpCardCount());
					}
					int flag2 = tbBizPlaceMemberOpHistoryMapper.insertSelective(tbBizPlaceMemberOpHistory);
					if (flag2 != 1) {
						throw new Exception("生成退款流水失败.");
					}
				} else {
					throw new Exception("修改会员卡失败.");
				}
			} else {
				throw new Exception("会员卡异常.");
			}
		} else {
			// 包场子订单退款
			TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
			TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample1.createCriteria();
			tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(reqPlaceRefundInfo.getPlaceOrderId());
			List<TbBizPlaceOrder> tbBizPlaceOrdersList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
			if (!StringUtils.isEmpty(tbBizPlaceOrdersList.get(0).getParentId())) {
				logger.debug("进入包场子订单退款流程");
				// 根据子订单查询父订单
				TbBizPlacePrivateOrder tbBizPlacePrivateOrder = tbBizPlacePrivateOrderMapper
						.selectByPrimaryKey(tbBizPlaceOrdersList.get(0).getParentId());
				String parentOrderId = tbBizPlacePrivateOrder.getPlaceOrderId();
				// 获取会员卡
				TbBizPlaceMemberOpHistoryExample example1 = new TbBizPlaceMemberOpHistoryExample();
				TbBizPlaceMemberOpHistoryExample.Criteria criteria1 = example1.createCriteria();
				criteria1.andOrderNoEqualTo(parentOrderId);
				List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList1 = this.tbBizPlaceMemberOpHistoryMapper
						.selectByExample(example1);
				if (CollectionUtils.isNotEmpty(tbBizPlaceMemberOpHistoryList1)) {
					String innerCardNo = tbBizPlaceMemberOpHistoryList1.get(0).getInnerCardNo();
					if (!StringUtils.isEmpty(innerCardNo)) {
						TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
						TbBizPlaceMemberCardExample.Criteria tbBizPlaceMemberCardCriteria = tbBizPlaceMemberCardExample
								.createCriteria();
						tbBizPlaceMemberCardCriteria.andInnerCardNoEqualTo(innerCardNo);
						List<TbBizPlaceMemberCard> tbBizPlaceMemberCardList = this.tbBizPlaceMemberCardMapper
								.selectByExample(tbBizPlaceMemberCardExample);
						tbBizPlaceMemberCardExample.setOrderByClause(" id for update ");
						TbBizPlaceMemberCard tbBizPlaceMemberCard = tbBizPlaceMemberCardList.get(0);
						BigDecimal cardTypeInitGiveAmount = tbBizPlaceMemberCard.getGiveAmount();
						BigDecimal initAmount = tbBizPlaceMemberCard.getSurplusAmount();
						BigDecimal initGiveAmount = tbBizPlaceMemberCard.getSurplusGiveAmount();
						// 储值，折扣卡，储值折扣卡退款
						if (tbBizPlaceMemberCard.getSurplusGiveAmount().compareTo(cardTypeInitGiveAmount) == -1) {
							// 会员卡赠送金额 < 卡类型初始赠送金额 退到赠送
							if (tbBizPlaceMemberCard.getSurplusGiveAmount().add(tbBizPlaceOrdersList.get(0).getCredit())
									.compareTo(cardTypeInitGiveAmount) == 1) {
								// 会员卡赠送余额+退款金额 > 卡类型初始金额
								BigDecimal initSurplusGiveAmount = tbBizPlaceMemberCard.getSurplusGiveAmount();
								// 赠送填满
								tbBizPlaceMemberCard.setSurplusGiveAmount(cardTypeInitGiveAmount);
								// 剩余退到本金
								tbBizPlaceMemberCard.setSurplusAmount(tbBizPlaceOrdersList.get(0).getCredit()
										.subtract(cardTypeInitGiveAmount.subtract(initSurplusGiveAmount)));
							} else {
								// 会员卡赠送余额+退款金额 <= 卡类型初始金额 直接退到赠送
								tbBizPlaceMemberCard.setSurplusGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount()
										.add(tbBizPlaceOrdersList.get(0).getCredit()));
							}
						} else {
							// 退款到本金
							tbBizPlaceMemberCard.setSurplusAmount(tbBizPlaceMemberCard.getSurplusAmount()
									.add(tbBizPlaceOrdersList.get(0).getCredit()));
						}
						int flag1 = tbBizPlaceMemberCardMapper.updateByPrimaryKey(tbBizPlaceMemberCard);
						if (flag1 == 1) {
							TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory = new TbBizPlaceMemberOpHistory();
							// 退款生成订单号
							String orderNo = CommonUtils.generateBusinessOrderNo(Constants.PLACE_SYSTEM, reqPlaceRefundInfo.getOperateUid());
							tbBizPlaceMemberOpHistory.setOrderNo(orderNo);
							tbBizPlaceMemberOpHistory.setRefundNo(tbBizPlaceMemberOpHistoryList1.get(0).getOrderNo());
							tbBizPlaceMemberOpHistory.setId(tbBizPlaceMemberOpHistoryList1.get(0).getId());
							tbBizPlaceMemberOpHistory
									.setCreateUid(tbBizPlaceMemberOpHistoryList1.get(0).getCreateUid());
							tbBizPlaceMemberOpHistory.setPlaceId(tbBizPlaceMemberOpHistoryList1.get(0).getPlaceId());
							tbBizPlaceMemberOpHistory.setUpdateTime(new Date());
							tbBizPlaceMemberOpHistory.setAmount(tbBizPlaceOrdersList.get(0).getCredit());
							tbBizPlaceMemberOpHistory.setCreateTime(new Date());
							tbBizPlaceMemberOpHistory.setInnerCardNo(tbBizPlaceMemberCard.getInnerCardNo());
							tbBizPlaceMemberOpHistory
									.setNo(CommonUtils.generateNo(tbBizPlaceMemberOpHistoryList1.get(0).getPlaceId()));
							tbBizPlaceMemberOpHistory
									.setOpTypeName(tbBizPlaceMemberOpHistoryList1.get(0).getOpTypeName() + "退款");
							tbBizPlaceMemberOpHistory.setUid(tbBizPlaceMemberOpHistoryList1.get(0).getUid());
							tbBizPlaceMemberOpHistory.setUserName(tbBizPlaceMemberOpHistoryList1.get(0).getUserName());
							tbBizPlaceMemberOpHistory.setMobile(tbBizPlaceMemberOpHistoryList1.get(0).getMobile());
							tbBizPlaceMemberOpHistory
									.setCardTypeName(tbBizPlaceMemberOpHistoryList1.get(0).getCardTypeName());
							tbBizPlaceMemberOpHistory.setMemo(tbBizPlaceMemberOpHistoryList1.get(0).getMemo());
							tbBizPlaceMemberOpHistory.setOpAfterAmount(tbBizPlaceMemberCard.getSurplusAmount());
							tbBizPlaceMemberOpHistory.setOpAfterGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount());
							tbBizPlaceMemberOpHistory.setState(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_YES);
							tbBizPlaceMemberOpHistory.setOpBeforeAmount(initAmount);
							tbBizPlaceMemberOpHistory.setOpBeforeGiveAmount(initGiveAmount);
							tbBizPlaceMemberOpHistory.setSourceType(PlaceConstants.SOURCE_TYPE_SCENEPAY);
							tbBizPlaceMemberOpHistory.setSrc(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_REFUND);
							tbBizPlaceMemberOpHistory.setType(PlaceConstants.PLACE_MEMBER_HISTORY_TYPE_RECHARGE);
							tbBizPlaceMemberOpHistory
									.setCardTypeId(tbBizPlaceMemberOpHistoryList1.get(0).getCardTypeId());
							tbBizPlaceMemberOpHistory
									.setOpBeforeNumbers(tbBizPlaceMemberOpHistoryList1.get(0).getOpAfterNumbers());
							tbBizPlaceMemberOpHistory
									.setOpAfterNumbers(tbBizPlaceMemberOpHistoryList1.get(0).getOpBeforeNumbers());

							int flag2 = tbBizPlaceMemberOpHistoryMapper.insertSelective(tbBizPlaceMemberOpHistory);
							if (flag2 != 1) {
								throw new Exception("生成退款流水失败.");
							}
						} else {
							throw new Exception("修改会员卡失败.");
						}
					} else {
						throw new Exception("会员卡异常.");
					}
				}
			} else {
				logger.debug("非会员退款.");
			}
		}
		if (numCardRefund && refundNum > 0) {
			// 插入退款流水
			ReqOrderHistory reqOrderHistory = placeOrderUtilService.memberTicket2ReqOrderHistory(newTbBizPlaceOrder,
					refundNum, (byte) 1, (byte) 0);
			placeOrderUtilService.insertOrderHistory(reqOrderHistory);
		} else {
			if (PlaceConstants.ORDER_REFUND_STATUS.equals(newTbBizPlaceOrder.getOrderStatus())
					&& PlaceConstants.REFUNDING_STATUS.equals(newTbBizPlaceOrder.getPayStatus())) {
				// 插入退款流水
				ReqOrderHistory reqOrderHistory = placeOrderUtilService
						.tbBizPlaceOrder2ReqOrderHistory(newTbBizPlaceOrder, refundPrice, (byte) 1, (byte) 0);
				placeOrderUtilService.insertOrderHistory(reqOrderHistory);
			}
		}
	
		
	}

	/**
	 * 购物车退款
	 * @param tbBizPlaceOrderItems
	 * @param newTbBizPlaceOrder
	 * @param tbBizPlacePayItems
	 * @param orderDiscountItems
	 * @param reqPlaceRefundInfo
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	private TbBizPlaceOrder shoppingCartRefund(List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,
			TbBizPlaceOrder tbBizPlaceOrder, List<TbBizPlacePayItem> tbBizPlacePayItems,
			List<RespPlaceOrderDiscountItem> orderDiscountItems, PlaceRefundInfo reqPlaceRefundInfo) throws Exception{
		String refundNo=CommonUtils.generateRefundNo(PlaceConstants.PLACE_REFUND_CODE);//生成退款单号
		
		if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())
				|| PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
			/** 扫码退款 **/
			tbBizPlaceOrder = this.placeScanRefund(refundNo,tbBizPlaceOrder, tbBizPlacePayItems, tbBizPlaceOrderItems,orderDiscountItems,reqPlaceRefundInfo);
		}
		
		//非扫码退
		if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_ALL){
			
			if (PlaceConstants.ORDER_REFUND_STATUS.equals(tbBizPlaceOrder.getOrderStatus())
					|| PlaceConstants.REFUNDING_STATUS.equals(tbBizPlaceOrder.getPayStatus())) {
				//已退款
				//修改订单明细、支付明细、优惠明细状态
				this.refundAllSuccessUpdateData(tbBizPlaceOrderItems, tbBizPlacePayItems, orderDiscountItems);
				
				//释放库存
				this.freeShoppingCartRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
				//记入退款单
				this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null,PlaceConstants.REFUND_ALL);
			}
		
		}else if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_PART){
			TbBizPlaceOrderItem orderItem=tbBizPlaceOrderItems.get(0);
			RespPlaceOrderDiscountItem discountItem=orderDiscountItems.get(0);
			TbBizPlacePayItem payItem=tbBizPlacePayItems.get(0);
			//已退款
			//1.修改订单明细 状态
		    int  surplusTicketCount=orderItem.getSurplusGoodsCount()-reqPlaceRefundInfo.getTicketCount();//退的门票
			if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
			    orderItem.setRefundtype(PlaceConstants.REFUND_PART);
				//释放库存
				this.freePlaceScanRefundPartStore(tbBizPlaceOrder, orderItem, reqPlaceRefundInfo.getTicketCount());
			}else{//退整单笔明细
				 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);	
				 
				 List<TbBizPlaceOrderItem> orderItems=new ArrayList<>();
				 orderItems.add(orderItem);
				//释放库存
				this.freeShoppingCartRefundAllStore(tbBizPlaceOrder, orderItems);
			}
			
			orderItem.setSurplusGoodsCount(surplusTicketCount);
			tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
			//整个订单明细都退了
			if(surplusTicketCount==0){
				//修改订单明细、支付明细、优惠明细状态
				this.refundPartSuccessUpdateData(orderItem, payItem, discountItem);
			}
			//记入退款单
			this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null,PlaceConstants.REFUND_PART);
			
			//5.整个订单所有明细都退了，修改订单状态
			TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
			tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
			tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
			List<TbBizPlaceOrderItem> orderItemsData=tbBizPlaceOrderItemMapper
					.selectByExample(tbBizPlaceOrderItemExample);
			if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
				tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
				tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
			}
			
		}
		
		
		return tbBizPlaceOrder;
	}

	/**
	 * 门票订单退款
	 * 
	 * @param tbBizPlaceOrderItems
	 * @param tbBizPlaceOrder
	 * @param tbBizPlacePayItems
	 * @throws Exception
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	private TbBizPlaceOrder ticketOrderRefund(List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,
			TbBizPlaceOrder tbBizPlaceOrder, List<TbBizPlacePayItem> tbBizPlacePayItems,List<RespPlaceOrderDiscountItem> orderDiscountItems,PlaceRefundInfo reqPlaceRefundInfo) throws Exception {
		String refundNo=CommonUtils.generateRefundNo(PlaceConstants.PLACE_REFUND_CODE);//生成退款单号
		
		if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())
				|| PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
			/** 扫码退款 **/
			tbBizPlaceOrder = this.placeScanRefund(refundNo,tbBizPlaceOrder, tbBizPlacePayItems, tbBizPlaceOrderItems,orderDiscountItems,reqPlaceRefundInfo);
		}
		
		//非扫码退
		if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_ALL){
			
			if (PlaceConstants.ORDER_REFUND_STATUS.equals(tbBizPlaceOrder.getOrderStatus())
					|| PlaceConstants.REFUNDING_STATUS.equals(tbBizPlaceOrder.getPayStatus())) {
				//已退款
				//修改订单明细、支付明细、优惠明细状态
				this.refundAllSuccessUpdateData(tbBizPlaceOrderItems, tbBizPlacePayItems, orderDiscountItems);
				
				// 修改门票统计
				placeTicketService.updateTicketStatistics(tbBizPlaceOrder, tbBizPlaceOrderItems);
				
				//记入退款单
				this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null,PlaceConstants.REFUND_ALL);
				
			}
		
		}else if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_PART){
			TbBizPlaceOrderItem orderItem=tbBizPlaceOrderItems.get(0);
			RespPlaceOrderDiscountItem discountItem=orderDiscountItems.get(0);
			TbBizPlacePayItem payItem=tbBizPlacePayItems.get(0);
			//已退款
			//1.修改订单明细 状态
		    int  surplusTicketCount=orderItem.getSurplusGoodsCount()-reqPlaceRefundInfo.getTicketCount();//退的门票
			if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
			    orderItem.setRefundtype(PlaceConstants.REFUND_PART);
				//4修改门票统计
				placeTicketService.updateTicketStatisticsByPart(tbBizPlaceOrder, orderItem,reqPlaceRefundInfo.getTicketCount());
			}else{//退整单笔明细
				 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
				//4  修改门票统计
				List<TbBizPlaceOrderItem> orderItems=new ArrayList<>();
				orderItems.add(orderItem);
				placeTicketService.updateTicketStatistics(tbBizPlaceOrder,orderItems);
			}
			
			orderItem.setSurplusGoodsCount(surplusTicketCount);
			tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
			//整个订单明细都退了
			if(surplusTicketCount==0){
				//修改订单明细、支付明细、优惠明细状态
				this.refundPartSuccessUpdateData(orderItem, payItem, discountItem);
			}
			//记入退款单
			this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null,PlaceConstants.REFUND_PART);
			
			//5.整个订单所有明细都退了，修改订单状态
			TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
			tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
			tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
			List<TbBizPlaceOrderItem> orderItemsData=tbBizPlaceOrderItemMapper
					.selectByExample(tbBizPlaceOrderItemExample);
			if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
				tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
				tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
			}
			
		}
		
		
		return tbBizPlaceOrder;
	}

	/**
	 * 全部退款成功，修改支付明细，订单明细，优惠明细状态
	 * @param tbBizPlaceOrderItems
	 * @param tbBizPlacePayItems
	 * @param orderDiscountItems
	 */
	private void refundAllSuccessUpdateData(List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,
			List<TbBizPlacePayItem> tbBizPlacePayItems, List<RespPlaceOrderDiscountItem> orderDiscountItems) {
		//修改支付明细状态
		for(TbBizPlacePayItem tbBizPlacePayItem:tbBizPlacePayItems){
			tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
			tbBizPlacePayItemMapper.updateByPrimaryKeySelective(tbBizPlacePayItem);
		}
		
		//修改订单明细状态
		for(TbBizPlaceOrderItem orderItem:tbBizPlaceOrderItems){
			orderItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
			orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
			orderItem.setSurplusGoodsCount(0);
			tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
		}
		
		//修改优惠状态
		for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
			discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
			placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
		}
	}

	private void refundPartSuccessUpdateData(TbBizPlaceOrderItem tbBizPlaceOrderItem,
			TbBizPlacePayItem tbBizPlacePayItem,RespPlaceOrderDiscountItem orderDiscountItem) {
		//修改支付明细状态
		tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
		tbBizPlacePayItemMapper.updateByPrimaryKeySelective(tbBizPlacePayItem);
		
		//修改订单明细状态
		tbBizPlaceOrderItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
		tbBizPlaceOrderItem.setRefundtype(PlaceConstants.REFUND_ALL);
		tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(tbBizPlaceOrderItem);
	
		//修改优惠状态
		orderDiscountItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
		placeOrderAdminMapper.updateDiscountItemByPK(orderDiscountItem);
	}

	/**
	 * 开单包场散客退款
	 * 
	 * @param tbBizPlaceOrderItems
	 * @param tbBizPlaceOrder
	 * @param tbBizPlacePayItems
	 * @throws Exception
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	private TbBizPlaceOrder billingAndBlockOrderRefund(List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,
			TbBizPlaceOrder tbBizPlaceOrder, List<TbBizPlacePayItem> tbBizPlacePayItems,List<RespPlaceOrderDiscountItem> orderDiscountItems,Integer refundMode,PlaceRefundInfo reqPlaceRefundInfo) throws Exception {
		String refundNo=CommonUtils.generateRefundNo(PlaceConstants.PLACE_REFUND_CODE);//生成退款单号
		
		if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())
				|| PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
			if (tbBizPlaceOrder.getUseType().byteValue() == PlaceConstants.ORDER_USE_BLOCK_BOOKING) {
				// 包场退款
				TbBizPlacePrivateOrder tbBizPlacePrivateOrder = tbBizPlacePrivateOrderMapper
						.selectByPrimaryKey(tbBizPlaceOrder.getParentId());

				tbBizPlaceOrder.setOrderNo(tbBizPlacePrivateOrder.getOrderNo());
				tbBizPlaceOrder = this.placeScanRefund(refundNo,tbBizPlaceOrder, tbBizPlacePayItems, tbBizPlaceOrderItems,
						orderDiscountItems,reqPlaceRefundInfo);
			} else {
				tbBizPlaceOrder = this.placeScanRefund(refundNo,tbBizPlaceOrder, tbBizPlacePayItems, tbBizPlaceOrderItems,orderDiscountItems,reqPlaceRefundInfo);
			}
		}
		
		if(refundMode==PlaceConstants.REFUND_ALL){
			if (PlaceConstants.ORDER_REFUND_STATUS.equals(tbBizPlaceOrder.getOrderStatus())
					|| PlaceConstants.REFUNDING_STATUS.equals(tbBizPlaceOrder.getPayStatus())) {
				// 已退款，
				//修改订单明细、支付明细、优惠明细状态
				this.refundAllSuccessUpdateData(tbBizPlaceOrderItems, tbBizPlacePayItems, orderDiscountItems);
				
				//释放库存
				this.refundUpdateUnitSalePlan(tbBizPlaceOrderItems, PlaceConstants.PLACE_PREORDER_STATUS);
				 this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null, PlaceConstants.REFUND_ALL);
			}
			
		}else{//部分退   
			//已退款
			TbBizPlaceOrderItem orderItem=tbBizPlaceOrderItems.get(0);
			RespPlaceOrderDiscountItem discountItem=orderDiscountItems.get(0);
			TbBizPlacePayItem payItem=tbBizPlacePayItems.get(0);
			
			orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
			orderItem.setSurplusGoodsCount(0);
			tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
			
			
			//退整单笔明细(开单包场，每个销售id生成一个明细，退也退整笔明细)
			this.refundPartSuccessUpdateData(orderItem,payItem,discountItem);

			//整个订单所有明细都退了，修改订单状态
			TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
			tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
			tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
		
			List<TbBizPlaceOrderItem> orderItemsData=tbBizPlaceOrderItemMapper
					.selectByExample(tbBizPlaceOrderItemExample);
			if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
				tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
				tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
			}
			
			//4.释放库存
			List<TbBizPlaceOrderItem> orderItems=new ArrayList<>();
			orderItems.add(orderItem);
			this.refundUpdateUnitSalePlan(orderItems, PlaceConstants.PLACE_PREORDER_STATUS);
			//记入退款单
			this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null,PlaceConstants.REFUND_PART);
		}
		
		return tbBizPlaceOrder;
	}

	/**
	 * 商品订单退款
	 * 
	 * @param tbBizPlaceOrderItems
	 * @param tbBizPlaceOrder
	 * @param tbBizPlacePayItems
	 * @throws Exception
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	private TbBizPlaceOrder goodsOrderRefund(List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,
			TbBizPlaceOrder tbBizPlaceOrder, List<TbBizPlacePayItem> tbBizPlacePayItems,List<RespPlaceOrderDiscountItem> orderDiscountItems,PlaceRefundInfo reqPlaceRefundInfo) throws Exception {
		String refundNo=CommonUtils.generateRefundNo(PlaceConstants.PLACE_REFUND_CODE);//生成退款单号
		
		if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())
				|| PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
			/** 扫码退款 **/
			tbBizPlaceOrder = this.placeScanRefund(refundNo,tbBizPlaceOrder, tbBizPlacePayItems, tbBizPlaceOrderItems,orderDiscountItems,reqPlaceRefundInfo);
		}
		
		if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_ALL){
			if (PlaceConstants.ORDER_REFUND_STATUS.equals(tbBizPlaceOrder.getOrderStatus())
					|| PlaceConstants.REFUNDING_STATUS.equals(tbBizPlaceOrder.getPayStatus())) {
				//修改订单明细、支付明细、优惠明细状态
				this.refundAllSuccessUpdateData(tbBizPlaceOrderItems, tbBizPlacePayItems, orderDiscountItems);
				
				// 释放库存
				placeGoodsService.addGoodsInventory(tbBizPlaceOrder, tbBizPlaceOrderItems);
				//添加退款单
				this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null, PlaceConstants.REFUND_ALL);
				
			}
			
		}else{//部分退
			TbBizPlaceOrderItem orderItem=tbBizPlaceOrderItems.get(0);
			RespPlaceOrderDiscountItem discountItem=orderDiscountItems.get(0);
			TbBizPlacePayItem payItem=tbBizPlacePayItems.get(0);
			//已退款
			//1.修改订单明细 状态
		    int surplusGoodsCount=orderItem.getSurplusGoodsCount()-reqPlaceRefundInfo.getRefundGoodsCount();//退的商品
			if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
			    orderItem.setRefundtype(PlaceConstants.REFUND_PART);
				// 4.释放库存
				placeGoodsService.addGoodsInventoryByPart(tbBizPlaceOrder, orderItem, reqPlaceRefundInfo.getRefundGoodsCount());
			}else{//退整单笔明细
				 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
				//4.释放库存
				List<TbBizPlaceOrderItem> orderItems=new ArrayList<>();
				orderItems.add(orderItem);
				placeGoodsService.addGoodsInventory(tbBizPlaceOrder, orderItems);
			}
			orderItem.setSurplusGoodsCount(surplusGoodsCount);
			tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
			
			//整个订单明细都退了，修改订单状态
			if(surplusGoodsCount==0){
				//修改订单明细、支付明细、优惠明细状态
				this.refundPartSuccessUpdateData(orderItem, payItem, discountItem);
			}
			//记入退款单
			this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_STATUS, new Date().getTime(), null,PlaceConstants.REFUND_PART);
			//5.整个订单所有明细都退了，修改订单状态
			TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
			tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
			tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
			List<TbBizPlaceOrderItem> orderItemsData=tbBizPlaceOrderItemMapper
					.selectByExample(tbBizPlaceOrderItemExample);
			if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
				tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
				tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
			}
			
		}
		
		return tbBizPlaceOrder;
	}

	
	@Override
	public TbBizPlaceOrder placeScanRefund(String refundNo,TbBizPlaceOrder tbBizPlaceOrder, List<TbBizPlacePayItem> tbBizPlacePayItems,
			List<TbBizPlaceOrderItem> tbBizPlaceOrderItems,List<RespPlaceOrderDiscountItem> orderDiscountItems,PlaceRefundInfo reqPlaceRefundInfo) throws Exception {
		BigDecimal refundAmount = new BigDecimal(0);
		refundAmount = refundAmount.add(tbBizPlaceOrder.getCredit());
		
		BigDecimal zeroAmount = new BigDecimal(0);
		int valid = refundAmount.compareTo(zeroAmount);
		if (valid == -1 || valid == 0) {
			throw new BizException("订单金额不支持退款");
		}
		
		APIRequest<ReqOrderRefund> orderRefundAPIRequest = new APIRequest<>();
		ReqOrderRefund reqOrderRefund = new ReqOrderRefund();
		reqOrderRefund.setSourceBusinessId(Constants.PLACE_SCANPAY_CODE);
		reqOrderRefund.setSourceSystemId(Constants.PLACE_SYSTEM_CODE);
		reqOrderRefund.setOrderNo(tbBizPlaceOrder.getOrderNo());
		reqOrderRefund.setReason("百灵鸟商家端订单管理退款");
		reqOrderRefund.setUid(tbBizPlaceOrder.getCreateUid());
		reqOrderRefund.setAmount(refundAmount);
		reqOrderRefund.setNeedAudit(false); // false不需要人工审核. true: 需要人工审核,
											// 根据业务需求设置
		reqOrderRefund.setOutTradeNo(tbBizPlaceOrder.getPlaceOrderId());
		reqOrderRefund.setOutTradeRefundNo(refundNo);// 业务系统退款流水号
		
		List<ReqRefundProduct> reqRefundProducts = new ArrayList<>();
		if(reqPlaceRefundInfo.getRefundType()==PlaceConstants.REFUND_ALL){
			for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems) {
				ReqRefundProduct refundProduct = new ReqRefundProduct();
				refundProduct.setPrice(tbBizPlaceOrderItem.getSalePrice());
				refundProduct.setProductId(tbBizPlaceOrderItem.getPlaceOrderId());
				refundProduct.setProductName(tbBizPlaceOrderItem.getGoodsName());
				if(tbBizPlaceOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
					refundProduct.setQuantity(tbBizPlaceOrderItem.getTotalGoodsCount());
				}else if(tbBizPlaceOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)){
					refundProduct.setQuantity(tbBizPlaceOrderItem.getTicketCount());
				}else if(tbBizPlaceOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)||tbBizPlaceOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)||tbBizPlaceOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){
					refundProduct.setQuantity(1);
				}
				
				reqRefundProducts.add(refundProduct);
			}
			
			reqOrderRefund.setRefundProductList(reqRefundProducts);

			List<ReqRefundPayInfo> reqRefundPayInfoList = new ArrayList<ReqRefundPayInfo>();
			ReqRefundPayInfo rmb = new ReqRefundPayInfo();
			if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
				rmb.setPayId(PayWayEnum.PAY_WAY_WEICHAT_SCAN.getPayId());
			} else if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN
					.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
				rmb.setPayId(PayWayEnum.PAY_WAY_ALIPAY_SCAN.getPayId());
			} 
			rmb.setRefundAmount(refundAmount);
			reqRefundPayInfoList.add(rmb);

			reqOrderRefund.setReqRefundPayInfos(reqRefundPayInfoList);

			orderRefundAPIRequest.setData(reqOrderRefund);
			 APIResponse<RespRefund> apiResponse = orderPayService.refund(orderRefundAPIRequest);
			logger.debug("========================退款回调数据:reqOrderSave:{}", JSON.toJSONString(apiResponse));
			try {
				if (apiResponse == null || !apiResponse.isRet()) {
					//退款异常
					tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUNDING_STATUS);
					tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
					//修改订单明细状态为退款中
					for(TbBizPlaceOrderItem orderItem:tbBizPlaceOrderItems){
						orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
						orderItem.setSurplusGoodsCount(0);
						orderItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
					}
					
					//修改优惠状态
					for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
						discountItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
					}
				
					//释放库存
					if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_SHOPPING_CART)){
						this.freeShoppingCartRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}else{
						//释放库存
						this.freePlaceScanRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}
					//记入退款单
				    this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUNDING_STATUS, null, null, PlaceConstants.REFUND_ALL);
				
				    throw new BizException(apiResponse.getErrmsg());
				}else if (RefundStatusEnum.SUCCESS.getCode().equals(apiResponse.getData().getResult())) {
					// 退款成功
					tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
					tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
					//全部退款成功，修改支付明细，订单明细，优惠明细状态
					this.refundAllSuccessUpdateData(tbBizPlaceOrderItems, tbBizPlacePayItems, orderDiscountItems);
					
					//释放库存
					if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_SHOPPING_CART)){
						this.freeShoppingCartRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}else{
						//释放库存
						this.freePlaceScanRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}
					
					//记入退款单
					this.addRefundOrder(refundNo,reqPlaceRefundInfo,PlaceConstants.ORDER_REFUND_STATUS, apiResponse.getData().getRefundTime(), apiResponse.getData().getOrderRefundNo(), PlaceConstants.REFUND_ALL);
				} else  if(RefundStatusEnum.APPLY_SUBMIT.getCode().equals(apiResponse.getData().getResult())){
					// 退款申请已提交(退款中)，回调退款成功,修改支付明细状态
					tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUNDING_STATUS);
					tbBizPlaceOrder.setPayStatus(PlaceConstants.PAY_STATUS);
					//修改订单明细状态
					for(TbBizPlaceOrderItem orderItem:tbBizPlaceOrderItems){
						orderItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						orderItem.setSurplusGoodsCount(0);//
						orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
					}
					
					//修改优惠状态
					for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
						discountItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
					}
					
					//释放库存
					if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_SHOPPING_CART)){
						this.freeShoppingCartRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}else{
						//释放库存
						this.freePlaceScanRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}
					//记入退款单
					this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUNDING_STATUS, null, null, PlaceConstants.REFUND_ALL);
				}else if(RefundStatusEnum.FAIL.getCode().equals(apiResponse.getData().getResult())){
					tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_FAIL_STATUS);
					tbBizPlaceOrder.setPayStatus(PlaceConstants.PAY_STATUS);
					for(TbBizPlaceOrderItem orderItem:tbBizPlaceOrderItems){
						orderItem.setStatus((int)PlaceConstants.ORDER_REFUND_FAIL_STATUS);
						orderItem.setSurplusGoodsCount(0);
						orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
					}
					
					//修改优惠状态
					for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
						discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_FAIL_STATUS);
						placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
					}
					
					//释放库存
					if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_SHOPPING_CART)){
						this.freeShoppingCartRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}else{
						//释放库存
						this.freePlaceScanRefundAllStore(tbBizPlaceOrder, tbBizPlaceOrderItems);
					}
					
					//记入退款单
					this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_FAIL_STATUS, null, null, PlaceConstants.REFUND_ALL);
					
				}
				
			} catch (Exception e) {
				throw new BizException(e.getMessage());
			}
			
			
		}else{//部分退
			
			Integer refundCount=new Integer(0);
			TbBizPlaceOrderItem orderItem=tbBizPlaceOrderItems.get(0);
			RespPlaceOrderDiscountItem discountItem=orderDiscountItems.get(0);
			TbBizPlacePayItem payItem=tbBizPlacePayItems.get(0);
			
			ReqRefundProduct refundProduct = new ReqRefundProduct();
			refundProduct.setPrice(refundAmount);
			refundProduct.setProductId(orderItem.getPlaceOrderId());
			refundProduct.setProductName(orderItem.getGoodsName());
			if(refundAmount.compareTo(payItem.getAmount())!=0){//退订单明细部分
				if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
					refundProduct.setQuantity(reqPlaceRefundInfo.getRefundGoodsCount());
					refundCount=reqPlaceRefundInfo.getRefundGoodsCount();
				}else if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)){
					refundProduct.setQuantity(reqPlaceRefundInfo.getTicketCount());
					refundCount=reqPlaceRefundInfo.getTicketCount();
				}else if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)||orderItem.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)||orderItem.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){
					refundProduct.setQuantity(1);
				}
			}else{//退整单笔明细
				if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
					refundProduct.setQuantity(orderItem.getTotalGoodsCount());
					refundCount=orderItem.getTotalGoodsCount();
				}else if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)){
					refundProduct.setQuantity(orderItem.getTicketCount());
					refundCount=orderItem.getTicketCount();
				}else if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)||orderItem.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)||orderItem.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){
					refundProduct.setQuantity(1);
				}
				
			}
			
			reqRefundProducts.add(refundProduct);
			reqOrderRefund.setRefundProductList(reqRefundProducts);

			List<ReqRefundPayInfo> reqRefundPayInfoList = new ArrayList<ReqRefundPayInfo>();
			ReqRefundPayInfo rmb = new ReqRefundPayInfo();
			if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_WXSCAN.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
				rmb.setPayId(PayWayEnum.PAY_WAY_WEICHAT_SCAN.getPayId());
			} else if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_ALISCAN
					.equals(tbBizPlacePayItems.get(0).getPayType().toString())) {
				rmb.setPayId(PayWayEnum.PAY_WAY_ALIPAY_SCAN.getPayId());
			} 
			rmb.setRefundAmount(refundAmount);
			reqRefundPayInfoList.add(rmb);

			reqOrderRefund.setReqRefundPayInfos(reqRefundPayInfoList);

			orderRefundAPIRequest.setData(reqOrderRefund);
			 APIResponse<RespRefund> apiResponse = orderPayService.refund(orderRefundAPIRequest);
			logger.debug("========================退款回调数据:reqOrderSave:{}", JSON.toJSONString(apiResponse));
			try {
				if (apiResponse == null || !apiResponse.isRet()) {
						//1.修改订单明细 
					    int  surplusCount=orderItem.getSurplusGoodsCount()-refundCount;
					if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
					     orderItem.setRefundtype(PlaceConstants.REFUND_PART);
					}else{
						 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
					}
					    orderItem.setSurplusGoodsCount(surplusCount);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);//1.修改订单明细 
						
						if(surplusCount==0){
						//修改订单明细状态
						orderItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
						//修改优惠状态
						discountItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
						}
					
					 //5.整个订单所有明细都退了，修改订单状态
					TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
					tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
					tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
					List<TbBizPlaceOrderItem> orderItemsData = tbBizPlaceOrderItemMapper
							.selectByExample(tbBizPlaceOrderItemExample);
					if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
						tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUNDING_STATUS);
						tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
					}
					
					//释放库存
					this.freePlaceScanRefundPartStore(tbBizPlaceOrder, orderItem, refundCount);
					//记入退款单
				  this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUNDING_STATUS, null, null, PlaceConstants.REFUND_PART);
				  throw new BizException(apiResponse.getErrmsg());
				}else if (RefundStatusEnum.SUCCESS.getCode().equals(apiResponse.getData().getResult())) {
					int  surplusCount=orderItem.getSurplusGoodsCount()-refundCount;
					if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
					     orderItem.setRefundtype(PlaceConstants.REFUND_PART);
					}else{
						 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
					}
					    orderItem.setSurplusGoodsCount(surplusCount);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);//1.修改订单明细 
						if(surplusCount==0){
							this.refundPartSuccessUpdateData(orderItem, payItem, discountItem);
						}
					
					 //5.整个订单所有明细都退了，修改订单状态
					TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
					tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
					tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
					List<TbBizPlaceOrderItem> orderItemsData = tbBizPlaceOrderItemMapper
							.selectByExample(tbBizPlaceOrderItemExample);
					if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
						tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
						tbBizPlaceOrder.setPayStatus(PlaceConstants.REFUNDING_STATUS);
					}
					
					//释放库存
					this.freePlaceScanRefundPartStore(tbBizPlaceOrder, orderItem, refundCount);
					//记入退款单
				    this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUNDING_STATUS, apiResponse.getData().getRefundTime(), apiResponse.getData().getOrderRefundNo(), PlaceConstants.REFUND_PART);
				}else if(RefundStatusEnum.APPLY_SUBMIT.getCode().equals(apiResponse.getData().getResult())){
					// 退款申请已提交(退款中)，回调退款成功,修改支付明细状态
					//1.修改订单明细 
				    int  surplusCount=orderItem.getSurplusGoodsCount()-refundCount;
					if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
					     orderItem.setRefundtype(PlaceConstants.REFUND_PART);
					}else{
						 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
					}
				    orderItem.setSurplusGoodsCount(surplusCount);
					tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);//1.修改订单明细 
					if(surplusCount==0){
						//修改订单明细状态
						orderItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
						//修改优惠状态
						discountItem.setStatus((int)PlaceConstants.ORDER_REFUNDING_STATUS);
						placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
					}
				
				 //5.整个订单所有明细都退了，修改订单状态
				TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
				tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
				tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);//未退款订单明细状态
				List<TbBizPlaceOrderItem> orderItemsData = tbBizPlaceOrderItemMapper
						.selectByExample(tbBizPlaceOrderItemExample);
				if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态为已退款
					tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUNDING_STATUS);
					tbBizPlaceOrder.setPayStatus(PlaceConstants.PAY_STATUS);
				}
				
				//释放库存
				this.freePlaceScanRefundPartStore(tbBizPlaceOrder, orderItem, refundCount);
				//记入退款单
				this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUNDING_STATUS, null, null, PlaceConstants.REFUND_PART);
				}else if(RefundStatusEnum.FAIL.getCode().equals(apiResponse.getData().getResult())){
					//1.修改订单明细 
				    int  surplusCount=orderItem.getSurplusGoodsCount()-refundCount;
				if(tbBizPlaceOrder.getCredit().compareTo(payItem.getAmount())!=0){//退订单明细部分
				     orderItem.setRefundtype(PlaceConstants.REFUND_PART);
				}else{
					 orderItem.setRefundtype(PlaceConstants.REFUND_ALL);
				}
				    orderItem.setSurplusGoodsCount(surplusCount);
					tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);//1.修改订单明细 
					if(surplusCount==0){
						//修改订单明细状态
						orderItem.setStatus((int)PlaceConstants.ORDER_REFUND_FAIL_STATUS);
						tbBizPlaceOrderItemMapper.updateByPrimaryKeySelective(orderItem);
						//修改优惠状态
						discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_FAIL_STATUS);
						placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
					}
				
				 //5.整个订单所有明细都退了，修改订单状态
				TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
				tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
				tbBizPlaceOrderItemExample.createCriteria().andStatusNotEqualTo((int)PlaceConstants.ORDER_REFUND_STATUS);
				List<TbBizPlaceOrderItem> orderItemsData = tbBizPlaceOrderItemMapper
						.selectByExample(tbBizPlaceOrderItemExample);
				if(orderItemsData==null){//退完订单下所有订单明细，修改订单状态
					tbBizPlaceOrder.setOrderStatus(PlaceConstants.ORDER_REFUND_FAIL_STATUS);
					tbBizPlaceOrder.setPayStatus(PlaceConstants.PAY_STATUS);
				}
				
				//释放库存
				this.freePlaceScanRefundPartStore(tbBizPlaceOrder, orderItem, refundCount);
				//记入退款单
			  this.addRefundOrder(refundNo,reqPlaceRefundInfo, PlaceConstants.ORDER_REFUND_FAIL_STATUS, null, null, PlaceConstants.REFUND_PART);
				}
			} catch (Exception e) {
				throw new BizException(e.getMessage());
			}
			
		}
		
		return tbBizPlaceOrder;
	}

	/**
	 * 释放扫码退款(全部退)库存
	 * @param tbBizPlaceOrder
	 * @param tbBizPlaceOrderItems
	 * @throws Exception
	 */
	private void freePlaceScanRefundAllStore(TbBizPlaceOrder tbBizPlaceOrder,
			List<TbBizPlaceOrderItem> tbBizPlaceOrderItems) throws Exception {
		if(tbBizPlaceOrder.getUseType()==PlaceConstants.ORDER_USE_GOODS){
			// 释放库存
			placeGoodsService.addGoodsInventory(tbBizPlaceOrder, tbBizPlaceOrderItems);
		}else if(tbBizPlaceOrder.getUseType()==PlaceConstants.ORDER_USE_TICKET){
			// 修改门票统计
			placeTicketService.updateTicketStatistics(tbBizPlaceOrder, tbBizPlaceOrderItems);		
		}else if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
				|| tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
			//释放库存
			this.refundUpdateUnitSalePlan(tbBizPlaceOrderItems, PlaceConstants.PLACE_PREORDER_STATUS);
		}
	}
	
	/**
	 * 释放购物车订单退款(全部退)库存
	 * @param tbBizPlaceOrder
	 * @param tbBizPlaceOrderItems
	 * @throws Exception
	 */
	private void freeShoppingCartRefundAllStore(TbBizPlaceOrder tbBizPlaceOrder,
			List<TbBizPlaceOrderItem> tbBizPlaceOrderItems)throws Exception{
		if(tbBizPlaceOrder.getUseType()==PlaceConstants.ORDER_USE_SHOPPING_CART){
			for(TbBizPlaceOrderItem orderItem:tbBizPlaceOrderItems){
				if(orderItem.getUseType()==PlaceConstants.ORDER_USE_GOODS){
					List<TbBizPlaceOrderItem> OrderGoodsItems=new ArrayList<>();
					OrderGoodsItems.add(orderItem);
					// 释放库存
					placeGoodsService.addGoodsInventory(tbBizPlaceOrder, OrderGoodsItems);
				}else if(orderItem.getUseType()==PlaceConstants.ORDER_USE_TICKET){
					// 修改门票统计
					List<TbBizPlaceOrderItem> OrderTicketItems=new ArrayList<>();
					OrderTicketItems.add(orderItem);
					placeTicketService.updateTicketStatistics(tbBizPlaceOrder, OrderTicketItems);		
				}else if(orderItem.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
						|| orderItem.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
					//释放库存
					List<TbBizPlaceOrderItem> OrderItems=new ArrayList<>();
					OrderItems.add(orderItem);
					this.refundUpdateUnitSalePlan(OrderItems, PlaceConstants.PLACE_PREORDER_STATUS);
				}
			}
		}
		
	}
	
	
	
	/**
	 * 释放扫码退款库存(部分退)
	 * @param tbBizPlaceOrder
	 * @param tbBizPlaceOrderItems
	 * @throws Exception
	 */
	private void freePlaceScanRefundPartStore(TbBizPlaceOrder tbBizPlaceOrder,
			TbBizPlaceOrderItem tbBizPlaceOrderItem,Integer refundCount) throws Exception {
		if(tbBizPlaceOrder.getUseType()==PlaceConstants.ORDER_USE_GOODS){
			// 释放库存
			placeGoodsService.addGoodsInventoryByPart(tbBizPlaceOrder, tbBizPlaceOrderItem, refundCount);;
		}else if(tbBizPlaceOrder.getUseType()==PlaceConstants.ORDER_USE_TICKET){
			// 修改门票统计
			placeTicketService.updateTicketStatisticsByPart(tbBizPlaceOrder, tbBizPlaceOrderItem, refundCount);		
		}else if(tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
				|| tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
			//释放库存
			List<TbBizPlaceOrderItem> tbBizPlaceOrderItems=new ArrayList<>();
			tbBizPlaceOrderItems.add(tbBizPlaceOrderItem);
			this.refundUpdateUnitSalePlan(tbBizPlaceOrderItems, PlaceConstants.PLACE_PREORDER_STATUS);
		}
	}

	/**
	 * 开单包场退款修改销售计划
	 * 
	 * @param tbBizPlaceOrderItems
	 */
	public void refundUpdateUnitSalePlan(List<TbBizPlaceOrderItem> tbBizPlaceOrderItems, Byte status) throws Exception {
		List<String> saleIds = new ArrayList<>();
		for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems) {
			saleIds.add(tbBizPlaceOrderItem.getSaleId());
		}
		TbBizPlaceUnitSalePlanExample example = new TbBizPlaceUnitSalePlanExample();
		example.createCriteria().andSaleIdIn(saleIds);
		List<TbBizPlaceUnitSalePlan> tbBizPlaceUnitSalePlans = tbBizPlaceUnitSalePlanMapper.selectByExample(example);
		List<UnitSalePlan> list = new ArrayList<>();
		for (TbBizPlaceUnitSalePlan tsp : tbBizPlaceUnitSalePlans) {
			UnitSalePlan up = new UnitSalePlan();
			up.setSaleId(tsp.getSaleId());
			list.add(up);
		}
		ReqPlaceOrderListVo reqPlaceOrderListVo = new ReqPlaceOrderListVo();
		reqPlaceOrderListVo.setUnitSalePlanIdList(list);
		reqPlaceOrderListVo.setStatus(status);
		
		 placeOrderService.updateSalePlanByPrimaryKeyList(reqPlaceOrderListVo);
	}
	
	
	/**
	 * 增加退款单
	 * @param reqPlaceRefundInfo
	 * @param refundStatus
	 * @param refundTime 支付中心退款时间
	 * @param refundOrderNo 支付中心退款单号
	 * @param refundType 全部还是部分退
	 */
	private void addRefundOrder(String refundNo,PlaceRefundInfo reqPlaceRefundInfo,Byte refundStatus,Long refundTime,String refundOrderNo,Integer refundType) {
		//记入退款明细表
		if(refundType==PlaceConstants.REFUND_PART){
			PlaceRefundItem placeRefundItem=new PlaceRefundItem();
			placeRefundItem.setCategoryId(reqPlaceRefundInfo.getCategoryId());
			placeRefundItem.setCredit(reqPlaceRefundInfo.getRefundAmount());//saleprice
			placeRefundItem.setGoodsCount(reqPlaceRefundInfo.getRefundGoodsCount());
			placeRefundItem.setGoodsId(reqPlaceRefundInfo.getGoodsId());
			placeRefundItem.setName(reqPlaceRefundInfo.getGoodsName());//
			placeRefundItem.setItemUuid(reqPlaceRefundInfo.getItemUuid());
			placeRefundItem.setRefundEggCount(reqPlaceRefundInfo.getRefundEggCount());
			placeRefundItem.setRefundNo(refundNo);
			placeRefundItem.setSaleId(reqPlaceRefundInfo.getSaleId());
			placeRefundItem.setUseType((int)reqPlaceRefundInfo.getUseType());
			placeRefundItem.setConsumeIime(reqPlaceRefundInfo.getConsumeTime());
			placeRefundItem.setCreateTime(new Date());
			placeRefundItem.setStartTime(reqPlaceRefundInfo.getStartTime());
			placeRefundItem.setEndTime(reqPlaceRefundInfo.getEndTime());
			placeRefundItem.setTicketCount(reqPlaceRefundInfo.getTicketCount());
			refundOrderMapper.insertRefundItem(placeRefundItem);
		}else{
			Map<String, Object> params=new HashMap<>();
			params.put("placeOrderId", reqPlaceRefundInfo.getPlaceOrderId());
			List<RespOrderItemDiscount> orderItemDiscounts=placeOrderAdminMapper.getOrderDiscountItem(params);
			for(RespOrderItemDiscount itemDiscount:orderItemDiscounts){
				PlaceRefundItem placeRefundItem=new PlaceRefundItem();
				placeRefundItem.setCategoryId(itemDiscount.getCategoryId());
				placeRefundItem.setCredit(itemDiscount.getSalePrice());//saleprice
				placeRefundItem.setGoodsCount(itemDiscount.getTotalGoodsCount());
				placeRefundItem.setGoodsId(itemDiscount.getGoodsId());
				placeRefundItem.setName(itemDiscount.getGoodsName());//
				placeRefundItem.setItemUuid(itemDiscount.getItemUuid());
				placeRefundItem.setRefundEggCount(itemDiscount.getEggCount());
				placeRefundItem.setRefundNo(refundNo);
				placeRefundItem.setSaleId(itemDiscount.getSaleId());
				placeRefundItem.setUseType(itemDiscount.getUseType());
				placeRefundItem.setConsumeIime(itemDiscount.getConsumeIime());
				placeRefundItem.setCreateTime(new Date());
				placeRefundItem.setStartTime(itemDiscount.getStartTime());
				placeRefundItem.setEndTime(itemDiscount.getEndTime());
				placeRefundItem.setTicketCount(itemDiscount.getTicketCount());
				refundOrderMapper.insertRefundItem(placeRefundItem);
				
			}
		}
		
		
		//记入退款订单
		PlaceRefundInfo placeRefundInfo=new PlaceRefundInfo();
		placeRefundInfo.setCustomerId(reqPlaceRefundInfo.getCustomerId());
		placeRefundInfo.setOperateUid(reqPlaceRefundInfo.getOperateUid());
		placeRefundInfo.setPlaceId(reqPlaceRefundInfo.getPlaceId());
		placeRefundInfo.setPlaceOrderId(reqPlaceRefundInfo.getPlaceOrderId());
		placeRefundInfo.setRefundEggPeriod(reqPlaceRefundInfo.getRefundEggPeriod());//退鸟蛋周期
		placeRefundInfo.setRefundNo(refundNo);
		placeRefundInfo.setRefundNotes(reqPlaceRefundInfo.getRefundNotes());
		placeRefundInfo.setRefundPayType(reqPlaceRefundInfo.getRefundPayType());//退款方式
		placeRefundInfo.setRefundPeriod(reqPlaceRefundInfo.getRefundPeriod());//退款周期
		placeRefundInfo.setRefundStatus((int)refundStatus);
		placeRefundInfo.setRefundTime(new Date(refundTime));
		placeRefundInfo.setOrderNo(refundOrderNo);//支付中心退款单号
		placeRefundInfo.setPayInfo(reqPlaceRefundInfo.getPayInfo());
		placeRefundInfo.setRefundType(refundType);
		placeRefundInfo.setUpdateTime(new Date());
		placeRefundInfo.setCreateTime(new Date());
		refundOrderMapper.insertRefundOrder(placeRefundInfo);
	}

	
	@Override
	public PageObj<List<RespPlaceOrderList>> getPlaceRefundDetailList(ReqPlaceRefundParam refundParam) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public PlaceRefundInfo searchPlaceRefundDetail(String refundNo) {
		// TODO Auto-generated method stub
		return null;
	}
}
